package gui.chatarea;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import utils.IconManager;
import utils.IconManager.IconType;

public class ChatTabComponent extends JPanel implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final class TabButton extends JButton {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private static Icon DEFAULT_ICON = new ImageIcon(IconManager.getIcon(IconType.CLOSE_TAB));
		private static Icon OVER_ICON = new ImageIcon(IconManager.getIcon(IconType.CLOSE_TAB_OVER));
		
		private static final MouseListener BUTTON_MOUSE_LISTENER = new MouseAdapter() {
	       public void mouseEntered(MouseEvent e) {
	            Component component = e.getComponent();
	            if (component instanceof AbstractButton) {
	                AbstractButton button = (AbstractButton) component;
	                button.setIcon(OVER_ICON) ;
	            }
	        }
	 
	        public void mouseExited(MouseEvent e) {
	            Component component = e.getComponent();
	            if (component instanceof AbstractButton) {
	                AbstractButton button = (AbstractButton) component;
	                button.setIcon(DEFAULT_ICON) ;
	            }
	        }
		};

		public TabButton () {
			super () ;
			/* Transparent. */
			setContentAreaFilled(false);
			setFocusable(false);
			setBorder(BorderFactory.createEmptyBorder()) ;
			/* Set the icon. */
			setIcon(DEFAULT_ICON) ;
			setSize(new Dimension(DEFAULT_ICON.getIconWidth(), DEFAULT_ICON.getIconHeight())) ;
			addMouseListener(BUTTON_MOUSE_LISTENER) ;
		}
		
	}
	
	private JTabbedPane tabPane ;
	private JButton closeButton ;
	private JLabel titleLabel ;

	public ChatTabComponent (JTabbedPane tabPane) throws Exception {
		super () ;
		if (tabPane == null) { 
			throw new Exception("JTabbedPane must not be null.") ; 
		}
		this.tabPane = tabPane ;
		/* Create a Label that will get it text from the tab component title. */
		this.titleLabel = new JLabel () {
           /**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			public String getText() {
                int i = ChatTabComponent.this.tabPane.indexOfTabComponent(ChatTabComponent.this);
                if (i != -1) {
                	return ChatTabComponent.this.tabPane.getTitleAt(i);
                }
                return null;
            }
		};
		this.titleLabel.setOpaque(false) ;
		/* Create the button. */
		this.closeButton = new TabButton() ;
		/* Put everything together. */
		setOpaque(false) ;
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS)) ;
		add(this.titleLabel) ;
		add(Box.createRigidArea(new Dimension(5, 0))) ;
		add(this.closeButton) ;
		this.closeButton.addActionListener(this) ;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		int index = this.tabPane.indexOfTabComponent(this) ;
		if (index != -1) {
			this.tabPane.remove(index) ;
		}
	}

}
